package bb.lanxing.lib.devices.bryton.ncs;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.Looper;

public abstract class NotificationExtension extends Service {
    public static final String ACTION_EXTENSION = "com.brytonsport.ncs.Extension";
    public static final int UPDATE_REASON_CONTENT_CHANGED = 4;
    public static final int UPDATE_REASON_INITIAL = 1;
    public static final int UPDATE_REASON_MANUAL = 6;
    public static final int UPDATE_REASON_PERIODIC = 2;
    public static final int UPDATE_REASON_SCREEN_ON = 5;
    public static final int UPDATE_REASON_SETTINGS_CHANGED = 3;
    public static final int UPDATE_REASON_UNKNOWN = 0;
    private IExtensionHost mHost;
    private volatile Looper mServiceLooper;
    private boolean mInitialized = false;
    private volatile Handler mServiceHandler = null;
    private final IBinder mBinder = new LocalBinder();

    protected abstract void onUpdateData(int i);

    public final void publishUpdate(ExtensionData extensionData) {
        this.mHost.publishUpdate(extensionData);
    }

    public void onInitialize(boolean z) {
        if (this.mServiceHandler == null) {
            HandlerThread handlerThread = new HandlerThread("NotificationExtension:" + getClass().getSimpleName());
            handlerThread.start();
            this.mServiceLooper = handlerThread.getLooper();
            this.mServiceHandler = new Handler(this.mServiceLooper);
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return this.mBinder;
    }

    public class LocalBinder extends Binder implements IExtension {
        public LocalBinder() {
        }

        @Override
        public void onInitialize(IExtensionHost iExtensionHost, boolean z) {
            NotificationExtension.this.mHost = iExtensionHost;
            if (!NotificationExtension.this.mInitialized) {
                NotificationExtension.this.onInitialize(z);
                NotificationExtension.this.mInitialized = true;
            }
        }

        @Override
        public void onUpdate(final int i) {
            if (!NotificationExtension.this.mInitialized) {
                return;
            }
            NotificationExtension.this.mServiceHandler.post(new Runnable() {
                @Override
                public void run() {
                    NotificationExtension.this.onUpdateData(i);
                }
            });
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        if (this.mServiceHandler == null) {
            HandlerThread handlerThread = new HandlerThread("NotificationExtension:" + getClass().getSimpleName());
            handlerThread.start();
            this.mServiceLooper = handlerThread.getLooper();
            this.mServiceHandler = new Handler(this.mServiceLooper);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mServiceHandler.removeCallbacksAndMessages(null);
        this.mServiceLooper.quit();
        this.mServiceHandler = null;
    }
}
